import QtQuick 2.15
import QtQuick.Controls 2.15
import "qrc:/qml/styles" as Styles

Item {
    id: agvIcon

    // 自定义属性
    property string agvId: ""
    property string agvStatus: "idle"  // idle, executing, charging, error, offline
    property real battery: 100        // 电量百分比 0-100
    property real angle: 0            // AGV朝向角度(弧度)
    property bool showLabel: true     // 是否显示标签
    property bool showBattery: true   // 是否显示电量
    property string size: "normal"    // small, normal, large

    // 尺寸计算
    property real iconSize: {
        switch (size) {
            case "small": return 20;
            case "large": return Styles.Theme.agvIconSizeLarge;
            default: return Styles.Theme.agvIconSize;
        }
    }

    // 主体尺寸
    width: iconSize + (showLabel ? labelText.width + Styles.Theme.spacingSmall : 0)
    height: Math.max(iconSize, showLabel ? labelText.height : 0)

    // 状态颜色
    property color statusColor: Styles.Theme.getAgvStatusColor(agvStatus)

    // 信号
    signal clicked(string agvId)
    signal doubleClicked(string agvId)

    // AGV主体图标
    Rectangle {
        id: agvBody
        width: iconSize
        height: iconSize
        radius: iconSize * 0.2
        color: statusColor
        border.color: Qt.darker(statusColor, 1.3)
        border.width: 1

        // AGV形状 - 简化的车辆轮廓
        Rectangle {
            width: parent.width * 0.6
            height: parent.height * 0.3
            anchors.centerIn: parent
            color: Qt.lighter(parent.color, 1.2)
            radius: 2

            // 方向指示器
            Rectangle {
                width: parent.width * 0.3
                height: parent.height * 0.6
                anchors.right: parent.right
                anchors.verticalCenter: parent.verticalCenter
                color: Styles.Theme.textOnPrimary
                radius: 1
            }
        }

        // 旋转变换（显示AGV朝向）
        transform: Rotation {
            origin.x: agvBody.width / 2
            origin.y: agvBody.height / 2
            angle: agvIcon.angle * 180 / Math.PI
        }

        // 状态动画效果
        SequentialAnimation on opacity {
            running: agvStatus === "executing"
            loops: Animation.Infinite
            NumberAnimation { from: 1.0; to: 0.7; duration: 800 }
            NumberAnimation { from: 0.7; to: 1.0; duration: 800 }
        }

        // 错误状态闪烁
        SequentialAnimation on opacity {
            running: agvStatus === "error"
            loops: Animation.Infinite
            NumberAnimation { from: 1.0; to: 0.3; duration: 400 }
            NumberAnimation { from: 0.3; to: 1.0; duration: 400 }
        }

        // 充电状态动画
        Rectangle {
            visible: agvStatus === "charging"
            width: 8
            height: 4
            anchors.top: parent.top
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.topMargin: -2
            color: Styles.Theme.warning
            radius: 1

            // 充电闪烁效果
            SequentialAnimation on opacity {
                running: parent.visible
                loops: Animation.Infinite
                NumberAnimation { from: 0.3; to: 1.0; duration: 600 }
                NumberAnimation { from: 1.0; to: 0.3; duration: 600 }
            }
        }

        // 鼠标交互
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            cursorShape: Qt.PointingHandCursor

            onClicked: agvIcon.clicked(agvId)
            onDoubleClicked: agvIcon.doubleClicked(agvId)

            // 悬停效果
            Rectangle {
                anchors.fill: parent
                radius: parent.radius
                color: "white"
                opacity: parent.containsMouse ? 0.2 : 0
                Behavior on opacity { NumberAnimation { duration: 150 } }
            }
        }
    }

    // 电量指示器
    Rectangle {
        visible: showBattery && (size !== "small")
        width: iconSize
        height: 3
        anchors.top: agvBody.bottom
        anchors.topMargin: 2
        anchors.horizontalCenter: agvBody.horizontalCenter
        color: Styles.Theme.surfaceVariant
        radius: 1

        Rectangle {
            width: parent.width * (battery / 100)
            height: parent.height
            radius: parent.radius
            color: {
                if (battery > 50) return Styles.Theme.success;
                if (battery > 20) return Styles.Theme.warning;
                return Styles.Theme.danger;
            }

            Behavior on width {
                NumberAnimation { duration: Styles.Theme.animationNormal }
            }
        }
    }

    // AGV ID标签
    Text {
        id: labelText
        visible: showLabel
        anchors.left: agvBody.right
        anchors.leftMargin: Styles.Theme.spacingSmall
        anchors.verticalCenter: agvBody.verticalCenter
        text: agvId
        color: Styles.Theme.textPrimary
        font.pixelSize: {
            switch (size) {
                case "small": return 10;
                case "large": return 14;
                default: return 12;
            }
        }
        font.weight: Styles.Theme.fontWeightMedium
    }

    // 工具提示
    ToolTip {
        visible: mouseArea.containsMouse
        delay: 800
        text: qsTr("AGV: %1\n状态: %2\n电量: %3%").arg(agvId).arg(getStatusText()).arg(Math.round(battery))

        function getStatusText() {
            switch (agvStatus) {
                case "idle": return "空闲";
                case "executing": return "执行中";
                case "charging": return "充电中";
                case "error": return "故障";
                case "offline": return "离线";
                default: return "未知";
            }
        }
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true
        propagateComposedEvents: true
    }

    // 平滑的位置动画
    Behavior on x {
        NumberAnimation { duration: Styles.Theme.animationNormal; easing.type: Easing.OutCubic }
    }

    Behavior on y {
        NumberAnimation { duration: Styles.Theme.animationNormal; easing.type: Easing.OutCubic }
    }

    // 角度动画
    Behavior on angle {
        NumberAnimation { duration: Styles.Theme.animationNormal; easing.type: Easing.OutCubic }
    }
}